package cn.jly.spring.boot.cloud;

import cn.jly.spring.boot.cloud.beans.Animal;
import cn.jly.spring.boot.cloud.beans.Customer;
import cn.jly.spring.boot.cloud.beans.Person;
import cn.jly.spring.boot.cloud.utils.SpringBeansUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

/**
 * @EnableJpaRepositories(queryLookupStrategy = QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND)
 * 配置JPA生成查询方法的策略，这种是默认配置，先查@Query注解标注的sql，如果没有则根据方法名构建
 * @author lanyangji
 */
@EnableScheduling // 定时任务
@EnableJpaRepositories(queryLookupStrategy = QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND)
@Controller
@ServletComponentScan // 扫描三大组件
@EnableConfigurationProperties // 允许属性配置
@SpringBootApplication
@ImportResource(locations = {"classpath:spring/spring-beans.xml"}) // 导入spring配置文件
public class SpringBootCloudApplication {
    /**
     * 日志
     */
    Logger logger = LoggerFactory.getLogger(SpringBootCloudApplication.class);

    @Autowired
    private ApplicationContext applicationContext;

    public static void main(String[] args) {
        SpringApplication.run(SpringBootCloudApplication.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext applicationContext) {
        return args -> Arrays.stream(applicationContext.getBeanDefinitionNames())
                .forEach(System.out::println);
    }

    @Bean
    public ApplicationRunner applicationRunner() {
        return new ApplicationRunner() {
            @Override
            public void run(ApplicationArguments args) throws Exception {
                System.out.println("系统配置参数 -> " + args);
            }
        };
    }

    @Autowired
    private Person person;

    @GetMapping("/person")
    @ResponseBody
    public Person getPerson() {
        System.out.println(logger.getClass());
        logger.info("person => {}", person);
        return person;
    }

    @GetMapping("/customer")
    @ResponseBody
    public Customer getPersonInXml() {
        return SpringBeansUtils.getBean(Customer.class);
    }

    @GetMapping("/animal")
    @ResponseBody
    public Animal getAnimal() {
        System.out.println("applicationContext = " + applicationContext);
        final Animal animal = applicationContext.getBean(Animal.class);
        System.out.println("animal = " + animal);
        return SpringBeansUtils.getBean(Animal.class);
    }
}
